﻿using UnityEditor;
using UnityEngine;
using System.Collections;

/// <summary>
/// Batch Command for Setting Texture In Inspector.
/// </summary>
public class wseTexture : ScriptableObject
{
	// ----------------------------------------------------------------------------
	/// <summary>
	/// Texture Format to GUI and Maximum Size is 1024
	/// </summary>
	[MenuItem("wsLibrary/Texture/Texture Format To GUI 1024")]
	static void ChangeTextureToAdvGUI1024()
	{
		SeletedChangeTextureToAdvanced(TextureImporterType.GUI, 1024);
	}

	/// <summary>
	/// Texture Format to GUI and Maximum Size is 2048
	/// </summary>
	[MenuItem("wsLibrary/Texture/Texture Format To GUI 2048")]
	static void ChangeTextureToAdvGUI2048()
	{
		SeletedChangeTextureToAdvanced(TextureImporterType.GUI, 2048);
	}

	/// <summary>
	/// Texture Format to GUI and Maximum Size is 4096
	/// </summary>
	[MenuItem("wsLibrary/Texture/Texture Format To GUI 4096")]
	static void ChangeTextureToAdvGUI4096()
	{
		SeletedChangeTextureToAdvanced(TextureImporterType.GUI, 4096);
	}

	/// <summary>
	/// Texture Format to Advaced and Maximum Size is 1024
	/// </summary>
	[MenuItem("wsLibrary/Texture/Texture Format To Advanced 1024")]
	static void ChangeTextureToAdv1024()
	{
		SeletedChangeTextureToAdvanced(TextureImporterType.Advanced, 1024);
	}

	/// <summary>
	/// Texture Format to Advaced and Maximum Size is 2048
	/// </summary>
	[MenuItem("wsLibrary/Texture/Texture Format To Advanced 2048")]
	static void ChangeTextureToAdv2048()
	{
		SeletedChangeTextureToAdvanced(TextureImporterType.Advanced, 2048);
	}

	/// <summary>
	/// Texture Format to Advaced and Maximum Size is 4096
	/// </summary>
	[MenuItem("wsLibrary/Texture/Texture Format To Advanced 4096")]
	static void ChangeTextureToAdv4096()
	{
		SeletedChangeTextureToAdvanced(TextureImporterType.Advanced, 4096);
	}

	/// <summary>
	/// main method for setting texture.
	/// </summary>
	/// <param name="type">texture type - GUI, Advance, etc.</param>
	/// <param name="size">maximum texture size</param>
	static void SeletedChangeTextureToAdvanced(TextureImporterType type, int size)
	{
		Object[] textures = GetSelectedTextures();
		Selection.objects = new Object[0];

		int count = 0;
		foreach (Texture2D texture in textures)
		{
			count++;
			string path = AssetDatabase.GetAssetPath(texture);
			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

			textureImporter.textureType = type;
			textureImporter.npotScale = TextureImporterNPOTScale.None;

			if (type == TextureImporterType.Advanced)
			{
				texture.wrapMode = TextureWrapMode.Repeat;
				textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;

				textureImporter.isReadable = true;
				textureImporter.mipmapEnabled = false;
				textureImporter.normalmap = false;
			}
			else
			{
				texture.wrapMode = TextureWrapMode.Clamp;
				textureImporter.textureFormat = TextureImporterFormat.AutomaticCompressed;
			}

			textureImporter.maxTextureSize = size;

			AssetDatabase.ImportAsset(path);

			Debug.Log(string.Format("[{0}/{1}] Texture Format Progress : {2}", count, textures.Length, path));
		}
	}

	/// <summary>
	/// get all the selected textures.
	/// </summary>
	/// <returns></returns>
	static Object[] GetSelectedTextures()
	{
		return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
	}
}
